import 'package:lib_common/lib_common.dart';

/// Base控件
abstract class BaseWidget extends ConsumerWidget {
  const BaseWidget({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return buildContent(context, ref);
  }

  Widget buildContent(BuildContext context, WidgetRef ref);
}

/// Base控件状态
abstract class BaseState<T extends ConsumerStatefulWidget, M extends BaseWidgetModel, S extends BasePageState> extends ConsumerState<T> {
  late M widgetModel;
  late S pageState;

  @override
  void initState() {
    super.initState();
    initModelAndState();
    widgetModel.onInit();
  }

  void initModelAndState();

  @override
  Widget build(BuildContext context) {
    WidgetState dataState = ref.watch(pageState.pageStateProvider);
    switch (dataState) {
      // 初始化
      case WidgetState.init:
        widgetModel.onInit();
        break;
      // 失败状态
      case WidgetState.error:
        var error = ref.watch(pageState.errorProvider);
        widgetModel.onError(error);
        return buildError(context, error);
      // 请求后空数据状态
      case WidgetState.noData:
        widgetModel.onNoData();
        return buildNoData(context);
      // build状态
      case WidgetState.build:
        widgetModel.onBuild(context);
      // 刷新状态
      case WidgetState.refreshing:
        widgetModel.onRefresh();
        break;
      // 加载状态
      case WidgetState.loading:
        widgetModel.onLoading();
        break;
      // 成功状态
      case WidgetState.success:
        widgetModel.onSuccess();
        break;
      default:
        break;
    }
    return buildView(context, dataState);
  }

  Widget buildView(BuildContext context, WidgetState dataState);

  Widget buildError(BuildContext context, dynamic errorCode) {
    return const Center(child: Text('Widget Error'));
  }

  Widget buildNoData(BuildContext context) {
    return const Center(child: Text('Widget NoData'));
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    widgetModel.didChangeDependencies();
  }

  @override
  void didUpdateWidget(covariant T oldWidget) {
    super.didUpdateWidget(oldWidget);
    widgetModel.didUpdateWidget(oldWidget);
  }

  @override
  void reassemble() {
    super.reassemble();
    widgetModel.reassemble();
  }

  @override
  void deactivate() {
    super.deactivate();
    widgetModel.deactivate();
  }

  @override
  void activate() {
    super.activate();
    widgetModel.activate();
  }

  @override
  void dispose() {
    super.dispose();
    widgetModel.dispose();
  }
}

/// Base业务处理
class BaseWidgetModel<T extends ConsumerStatefulWidget> {

  void onBuild(BuildContext context) {
    LogUtils.v("$runtimeType: onBuild");
  }

  void onInit() {
    LogUtils.v("$runtimeType: onInit");
  }

  void onRefresh() {
    LogUtils.v("$runtimeType: onRefresh");
  }

  void onLoading() {
    LogUtils.v("$runtimeType: onLoading");
  }

  void onSuccess() {
    LogUtils.v("$runtimeType: onSuccess");
  }

  void onNoData() {
    LogUtils.v("$runtimeType: onNoData");
  }

  void onError(dynamic error) {
    LogUtils.v("$runtimeType: onError-->${error.runtimeType}:$error");
  }

  void didChangeDependencies() {
    LogUtils.v("$runtimeType: didChangeDependencies");
  }

  void didUpdateWidget(T oldWidget) {
    LogUtils.v("$runtimeType: didUpdateWidget");
  }

  void reassemble() {
    LogUtils.v("$runtimeType: reassemble");
  }

  void deactivate() {
    LogUtils.v("$runtimeType: deactivate");
  }

  void activate() {
    LogUtils.v("$runtimeType: activate");
  }

  void dispose() {
    LogUtils.v("$runtimeType: dispose");
  }
}

/// BaseWidget状态提供器
class BasePageState {
  // 状态Provider
  final pageStateProvider = StateProvider<WidgetState>((ref) => WidgetState.build);

  // 内容Provider
  final errorProvider = Provider<dynamic>((ref) => null);
}

/// 页面模板
class SimplePage extends BaseWidget {
  SimplePage({super.key});

  final pageState = SimplePageState();

  @override
  Widget buildContent(BuildContext context, WidgetRef ref) {
    final widgetModel = SimpleWidgetModel(ref, pageState);
    return Scaffold(
        appBar: AppBar(
            elevation: 0.0,
            centerTitle: true,
            title: GestureDetector(child: Consumer(builder: (context, ref, child) {
              var title = ref.watch(pageState.titleProvider.notifier);
              return Text(title.state);
            }), onTap: () {
              widgetModel.updateTitle();
            })));
  }
}

class SimpleStatePage extends ConsumerStatefulWidget {
  const SimpleStatePage({super.key});

  @override
  ConsumerState<ConsumerStatefulWidget> createState() => SimpleState();
}

class SimpleState extends BaseState<SimpleStatePage, SimpleWidgetModel, SimplePageState> {
  @override
  void initModelAndState() {
    pageState = SimplePageState();
    widgetModel = SimpleWidgetModel<SimpleStatePage>(ref, pageState);
  }

  @override
  Widget buildView(BuildContext context, WidgetState dataState, {dynamic errorCode}) {
    return Scaffold(
        appBar: AppBar(
            title: GestureDetector(child: Consumer(builder: (context, ref, child) {
      var title = ref.watch(pageState.titleProvider.notifier);
      return Text(title.state);
    }), onTap: () {
      widgetModel.updateTitle();
    })));
  }

  @override
  Widget buildError(BuildContext context, dynamic errorCode) {
    return buildView(context, WidgetState.error, errorCode: errorCode);
  }

  @override
  Widget buildNoData(BuildContext context) {
    return buildView(context, WidgetState.noData);
  }
}

class SimplePageState extends BasePageState {
  final titleProvider = StateProvider((ref) => "标题1");
}

class SimpleWidgetModel<T extends ConsumerStatefulWidget> extends BaseWidgetModel<T> {
  final WidgetRef ref;

  final SimplePageState state;

  SimpleWidgetModel(this.ref, this.state);

  void updateTitle() {
    var titleName = ref.read(state.titleProvider.notifier).state;
    if (titleName == "标题1") {
      titleName = "标题2";
    } else {
      titleName = "标题1";
    }
    ref.refresh(state.titleProvider.notifier).state = titleName;
  }
}
